#include <iostream>
#include <string>
#include <unistd.h>
#include <ctype.h>
#include <unordered_map>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Log.hpp"

struct sockaddr_in server;

static void Usage(const std::string porc)
{
  std::cout << "Usage:\n\t" << porc << " port [ip]" << std::endl;
}
//  ./server ip port
int main(int argc, char *argv[])
{
  if(argc != 2 && argc != 3)
  {
    Usage("Client");
    exit(3);
  }
  std::string ip = argv[1];
  uint16_t port = atoi(argv[2]);

  uint32_t sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  assert(sockfd > 0);

  server.sin_family = AF_INET;
  server.sin_port = htons(port);
  server.sin_addr.s_addr = inet_addr(ip.c_str());

  std::string buffer;
  while(true)
  {
    std::cout << "Please Enter# ";
    getline(std::cin, buffer);

    sendto(sockfd, buffer.c_str(), buffer.size(), 0, (const struct sockaddr*)&server, sizeof(server));
    //首次调用会自动绑定port和ip
  }
  close(sockfd);

  return 0;
}
